package com.htkj.demo.okhttp;

import java.io.File;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class CommonRequest {
    private static final MediaType TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

    private static final MediaType TYPE_STREAM = MediaType.parse("application/octet-stream");

    /**
     * 创建 Get 请求，将参数拼装成Url
     *
     * @return
     */
    public static Request createGetRequest(String url, List<RequestParams> paramsList) {
        String realUrl = url;
        Request.Builder requestBuilder = new Request.Builder();
        StringBuffer sb = new StringBuffer(url);
        if (paramsList != null) {
            sb.append("?");
            for (RequestParams params : paramsList) {
                sb.append(params.key).append("=").append(params.value).append("&");
            }
            realUrl = sb.substring(0, sb.length() - 1);
        }
        Request request = requestBuilder.url(realUrl).get().build();
        return request;
    }

    /**
     * 创建 Post 请求，填充Form表单
     *
     * @param url
     * @param paramsList
     * @return
     */
    public static Request createPostRequest(String url, List<RequestParams> paramsList) {
        Request.Builder requestBuilder = new Request.Builder();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if (paramsList != null) {
            for (RequestParams params : paramsList) {
                formBodyBuilder.add(params.key, params.value);
            }
        }
        RequestBody requestBody = formBodyBuilder.build();
        Request request = requestBuilder.url(url).post(requestBody).build();
        return request;
    }

    /**
     * 创建 Post json 请求，
     *
     * @param url
     * @param jsonStr
     * @return
     */
    public static Request createPostJsonRequest(String url, String jsonStr) {
        Request.Builder requestBuilder = new Request.Builder();
        RequestBody requestBody = RequestBody.create(TYPE_JSON, jsonStr);
        Request request = requestBuilder.url(url).post(requestBody).build();
        return request;
    }

    /**
     * post 上传文件
     *
     * @param url
     * @param file
     * @return
     */
    public static Request createPostFileRequest(String url, File file) {
        Request.Builder requestBuilder = new Request.Builder();
        RequestBody requestBody = RequestBody.create(TYPE_STREAM, file);
        Request request = requestBuilder.url(url).post(requestBody).build();
        return request;
    }

    /**
     * post上传文件并提交表单
     *
     * @param url        服务端处理请求的url
     * @param fileKey    表单中上传文件域的名字，服务端定义的File变量，名称要和这个相同。
     * @param file       要上传的文件
     * @param paramsList 要提交的表单数据
     * @return
     */
    public static Request createMultiPostRequest(String url, String fileKey, File file, List<RequestParams> paramsList) {
        MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
        for (RequestParams params : paramsList) {
            multipartBodyBuilder.addFormDataPart(params.key, params.value);
        }
        //文件上传enctype必须设置为 multipart/form-data
        MultipartBody multipartBody = multipartBodyBuilder.setType(MultipartBody.FORM)
                .addFormDataPart(fileKey, file.getName(), RequestBody.create(TYPE_STREAM, file))
                .build();
        Request request = new Request.Builder().url(url).post(multipartBody).build();
        return request;
    }

}
